# -*- coding: utf-8 -*-
# !/usr/bin/env python
from rest_framework.views import APIView
from components.logs_manage.dao import get_deploy_soft_log_info
from utils.mysql_utils import sql_decrypt
from utils.ssh_utils import SCPMet
from utils.views import Result, ReturnClass


class LogInfo(APIView):
    def get(self, request):
        data = request.query_params

        soft_info = get_deploy_soft_log_info(data['soft_id'])
        if not soft_info:
            return Result.fail_response(msg='查询失败，可能未维护日志路径，请检查配置信息')

        soft_info['resource_user_pass'] = sql_decrypt(soft_info['resource_user_pass'])
        ssh_obj = SCPMet()
        ssh_obj.set_info(soft_info)
        ssh_obj.connect()
        text_obj = ssh_obj.open_file_text(soft_info['soft_log_path'], 1000)
        ssh_obj.close()
        if ReturnClass.if_ok(text_obj):
            text = ReturnClass.get_value(text_obj)
        else:
            return Result.fail_response(data=text_obj['data'],msg='读取文件失败')


        return Result.success_response(data=text, msg='请求成功')
